package com.agriculture.service.impl;

import com.agriculture.dto.CommentDto;
import com.agriculture.dto.CommonSonDto;
import com.agriculture.entity.Category;
import com.agriculture.entity.Comment;
import com.agriculture.entity.Good;
import com.agriculture.entity.User;
import com.agriculture.mapper.CommentMapper;
import com.agriculture.service.CommentService;
import com.agriculture.service.GoodService;
import com.agriculture.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private UserService userService;

    @Autowired
    private GoodService goodService;

    @Autowired
    private CommentMapper commentMapper;



    /**
     * 根据 id 查询某个商品的全部评论及子评论
     * @param goodId
     * @return
     */
    @Override
    public List<CommentDto> findByGoodId(Long goodId) {

        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getGoodId, goodId);
        wrapper.orderByDesc(Comment::getCreateTime);
        List<Comment> list = list(wrapper);

        List<CommentDto> commentDtoList = list.stream().map((item) -> {
            CommentDto commentDto = new CommentDto();
            BeanUtils.copyProperties(item,commentDto);

            Long userId = item.getUserId();

            //根据id查询对象
            User user = userService.getById(userId);
            Good good = goodService.getById(goodId);

            // 根据 parent_id 查询子评论
            List<Comment> sons = commentMapper.selectById(item.getId());
            List<CommonSonDto> commentDtoListSons = sons.stream().map((son) -> {
                CommonSonDto commentDtoSons = new CommonSonDto();
                BeanUtils.copyProperties(son, commentDtoSons);

                Long userSonId = son.getUserId();
                User userSon = userService.getById(userSonId);
                Good goodSon = goodService.getById(goodId);
                if(userSon != null){
                    String userNameSon = userSon.getName();
                    commentDtoSons.setUserName(userNameSon);
                }
                if(goodSon != null){
                    String goodNameSon = goodSon.getName();
                    commentDtoSons.setGoodName(goodNameSon);
                }
                return commentDtoSons;
            }).collect(Collectors.toList());


            if(user != null){
                String userName = user.getName();
                commentDto.setUserName(userName);
            }
            if(good != null){
                String goodName = good.getName();
                commentDto.setGoodName(goodName);
            }
            if(!commentDtoListSons.isEmpty()){
                commentDto.setSons(commentDtoListSons);
            }
            return commentDto;
        }).collect(Collectors.toList());

        return commentDtoList;
    }

    /**
     * 查询所有评论
     * @return
     */
    @Override
    public List<CommentDto> getAll() {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Comment::getCreateTime);
        List<Comment> list = list(queryWrapper);

        List<CommentDto> commentDtoList = list.stream().map((item) -> {
            CommentDto commentDto = new CommentDto();
            BeanUtils.copyProperties(item,commentDto);

            Long userId = item.getUserId();
            Long goodId = item.getGoodId();

            //根据id查询对象
            User user = userService.getById(userId);
            Good good = goodService.getById(goodId);

            // 根据 parent_id 查询子评论
            List<Comment> sons = commentMapper.selectById(item.getId());
            List<CommonSonDto> commentDtoListSons = sons.stream().map((son) -> {
                CommonSonDto commentDtoSons = new CommonSonDto();
                BeanUtils.copyProperties(son, commentDtoSons);

                Long userSonId = son.getUserId();
                User userSon = userService.getById(userSonId);
                Good goodSon = goodService.getById(goodId);
                if(userSon != null){
                    String userNameSon = userSon.getName();
                    commentDtoSons.setUserName(userNameSon);
                }
                if(goodSon != null){
                    String goodNameSon = goodSon.getName();
                    commentDtoSons.setGoodName(goodNameSon);
                }
                return commentDtoSons;
            }).collect(Collectors.toList());

            if(user != null){
                String userName = user.getName();
                commentDto.setUserName(userName);
            }
            if(good != null){
                String goodName = good.getName();
                commentDto.setGoodName(goodName);
            }
            if(!commentDtoListSons.isEmpty()){
                commentDto.setSons(commentDtoListSons);
            }
            return commentDto;
        }).collect(Collectors.toList());

        return commentDtoList;
    }
}






